
#region copyright notice
/* ====================================================================
 * The UncleGED Open Source Software License
 *
 * Copyright (c) 1999-2009 D. M. DeBacker. All rights reserved.
 * http://gatheringleaves.org/uged
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Products derived from this software may not be called "UncleGED",
 *    nor may "UncleGED" appear in their name, without prior written
 *    permission of D. M. DeBacker.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL D. M. DEBACKER BE LIABLE FOR ANY 
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */
#endregion

using System;
using System.Globalization;

namespace UncleGED.Utils
{
	/// <summary>
	/// Class containing VB string-style methods that wrap .NET string methods
	/// </summary>
	/// <remarks>
	/// The only reason this class even exists because the original code was witten in VB6 and the functions made the port much easier.
	/// This should have been implemented as a static class, but wasn't.
	/// </remarks>
	/// <tocexclude/>
	public class StringFunctions
	{ 
		/// <summary>
		/// Constructor for string functions
		/// </summary>
		public StringFunctions()
		{
		}

		/// <summary>
		/// Mimics the BASIC Left function
		/// </summary>
		public string Left(string inputString, int strLen)
		{
			if (inputString.Length > strLen)
			{
				string retValue = inputString.Substring(0, strLen);
				return retValue;
			} 
			else 
			{
				return inputString;		 
			}
		}

		/// <summary>
		/// Mimics the BASIC Right function
		/// </summary>
		public string Right(string inputString, int strLen)
		{
			if (inputString.Length > strLen)
			{
				string retValue = inputString.Substring(inputString.Length - strLen, strLen);
				return retValue;
			}						
			else 
			{
				return inputString;		 
			}

		}

		/// <summary>
		/// Mimics the BASIC Mid function
		/// </summary>
		public string Mid(string inputString, int startIndex, int strLen)
		{
         if (inputString.Length < 2)
         {
            return inputString;
         }
         string retValue = inputString.Substring(startIndex - 1, strLen);
			return retValue;
		}

		/// <summary>
		/// Mimics the BASIC Mid function
		/// </summary>
		public string Mid(string inputString, int startIndex)
		{
         if (inputString.Length < 2)
         {
            return inputString;
         }
         string retValue = inputString.Substring(startIndex - 1);
			return retValue;
		}

		/// <summary>
		/// Mimics the BASIC Len function
		/// </summary>
		public int Len(string inputString)
		{
			return inputString.Length;
		}

		/// <summary>
		/// Mimics the BASIC UCase function
		/// </summary>
		public string UCase(string inputString)
		{
			return inputString.ToUpper();
		}

		/// <summary>
		/// Mimics the BASIC LCase function
		/// </summary>
		public string LCase(string inputString)
		{
			return inputString.ToLower();
		}

		/// <summary>
		/// Mimics the BASIC InStr function
		/// </summary>
		public int InStr(string String1, string String2)
		{

			int retValue = String1.IndexOf(String2) + 1;
			return retValue;
		}

		/// <summary>
		/// Mimics the BASIC InStr function
		/// </summary>
		public int InStr(int Start, string String1, string String2)
		{

			int retValue = String1.IndexOf(String2, Start - 1) + 1;
			return retValue;
		}

		/// <summary>
		/// Mimics the BASIC Trim function
		/// </summary>
		public string Trim(string inputString)
		{
			try
			{
				if (inputString.Length > 0)
				{
					return inputString.Trim();
				} 
				else 
				{
					return "";
				}
			} 
			catch
			{
				return "";
			}
		}

		/// <summary>
		/// Given a date string returns a DateTime value
		/// </summary>
		/// <param name="dateString">Formatted date string</param>
		public DateTime DateValue(string dateString)
		{
			DateTime dateValue = System.DateTime.Parse(dateString);
			return dateValue;
			
		}


		/// <summary>
		/// Mimics the BASIC IsDate function
		/// </summary>
		public bool IsDate(string inValue)
		{
			bool bValid;

			try 
			{
				DateTime myDT = DateTime.Parse(inValue);
				bValid = true;
			}
			catch //(FormatException e) 
			{
				bValid = false;
			}
				
         return bValid;
		}

		/// <summary>
		/// Formats a single (float) using the specified format
		/// </summary>
		/// <param name="number">the number to format</param>
		/// <param name="format">the format for the returned string</param>
      public string FormatSingle(Single number, string format)
      {
         string msg;

         msg = number.ToString(format);

         return msg;
      }
	}
}
